package com.yonyou.lxp.xutils3test.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yonyou.lxp.xutils3test.adapter.ViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2015/7/6.
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder>{
    private LayoutInflater mInflater;
    private Context mContext;
    private List<T> mDatas;
    private int mItemLayoutId = 0;

    public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        final ViewHolder viewHolder = getViewHolder(position, null,
                parent);


        if (viewHolder != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null)
                        onItemClickListener.onItemClick(viewHolder.getAdapterPosition(), viewHolder.getItemId(), v);
                }
            });
//            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    if (onItemLongClickListener == null)
//                        return false;
//                    onItemLongClickListener.onLongClick(viewHolder.getAdapterPosition(), viewHolder.getItemId(), v);
//                    return true;
//                }
//            });
        }

        return viewHolder;
    }


    /**
     * 绑定数据
     *
     * @param viewHolder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        convert(viewHolder, getItem(position));


//        // 如果设置了回调，则设置点击事件
//        if (mOnItemClickLitener != null) {
//            if(viewHolder.mConvertView!=null){
//                viewHolder.mConvertView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mOnItemClickLitener.onItemClick(viewHolder.mConvertView, position);
//                    }
//                });
//
//                viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        mOnItemClickLitener.onItemLongClick(viewHolder.mConvertView, position);
//                        return false;
//                    }
//                });
//            }
//
//        }
    }

    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 数据绑定
     * @param helper
     * @param item
     */
    public abstract void convert(ViewHolder helper, T item);

    /**
     * 创建ViewHolder
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId);
    }


//    /**
//     * 监听接口OnItemClickLitener
//     */
//    public interface OnItemClickLitener {
//        void onItemClick(View view, int position);
//
//        void onItemLongClick(View view, int position);
//    }
//
//    private OnItemClickLitener mOnItemClickLitener;
//
//    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
//        this.mOnItemClickLitener = mOnItemClickLitener;
//    }

    /**
     * adapter item on click
     */
    public interface OnItemClickListener {
        void onItemClick(int position, long id, View view);
    }

    public final void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    private OnItemClickListener onItemClickListener;
}
